<?php
class sub_table extends CI_Model{

	function getCategory($providerId = 0) {
		if ($providerId === 0) {
			$query = $this->db->get ( 'category' );
			return $query->result_array ();
		} else {
			$sql = 'SELECT category_id
			FROM provider_category JOIN provider ON provider.id = provider_id
			JOIN category ON category_id = category.id
			WHERE provider_id ='.$providerId;
			$query = $this->db->query ($sql);
			return $query->result_array ();
		}
	}


	function getTimePolicy($providerId = 0) {
		if ($providerId === 0) {
			$query = $this->db->get ( 'timepolicy' );
			return $query->result_array ();
		} else {
			$sql = 'SELECT timepolicy_id
			FROM provider_timepolicy JOIN provider ON provider.id = provider_id
			JOIN timepolicy ON timepolicy_id = timepolicy.id
			WHERE provider_id ='.$providerId;
			$query = $this->db->query ($sql);
			return $query->result_array ();
		}
	}

	function getPricePolicy($providerId = 0) {
		if ($providerId === 0) {
			$query = $this->db->get ( 'pricepolicy' );
			return $query->result_array ();
		} else {
			$sql = 'SELECT pricepolicy_id
			FROM provider_pricepolicy JOIN provider ON provider.id = provider_id
			JOIN pricepolicy ON pricepolicy_id = pricepolicy.id
			WHERE provider_id ='.$providerId;
			$query = $this->db->query ($sql);
			return $query->result_array ();
		}
	}
	function getLevel(){
		$sql = 'SELECT DISTINCT level FROM provider ORDER BY level ASC';
		$query = $this->db->query ($sql);
		return $query->result_array ();
	}
	function getProvince(){
		$sql = 'SELECT DISTINCT province FROM provider ORDER BY province ASC';
		$query = $this->db->query ($sql);
		return $query->result_array ();
	}


	function deletePricePolicy($providerId){
		$sqlDelete = 'DELETE FROM provider_pricepolicy WHERE provider_id ='.$providerId;
		return $this->db->query ($sqlDelete);
	}

	function insertPricePolicy($providerId,$pricePolicy){
		$sql = 'INSERT INTO provider_pricepolicy(provider_id,pricepolicy_id) VALUES('.$providerId.','.$pricePolicy.') ';
		return $this->db->query ($sql);
	}


	function deleteTimePolicy($providerId){
		$sqlDelete = 'DELETE FROM provider_timepolicy WHERE provider_id ='.$providerId;
		return $this->db->query ($sqlDelete);
	}
	function insertTimePolicy($providerId,$timePolicy){
		$sql = 'INSERT INTO provider_timepolicy(provider_id,timepolicy_id) VALUES('.$providerId.','.$timePolicy.') ';
		return $this->db->query ($sql);
	}

	function deleteCategory($providerId){
		$sqlDelete = 'DELETE FROM provider_category WHERE provider_id ='.$providerId;
		return $this->db->query ($sqlDelete);
	}
	function insertCategory($providerId,$category){
		$sql = 'INSERT INTO provider_category(provider_id,category_id) VALUES('.$providerId.','.$category.') ';
		return $this->db->query ($sql);
	}

}
?>